#include "ReturnBookDialog.h"

#include <wx/wx.h>
#include <wx/textctrl.h>
#include <wx/button.h>
#include <wx/stattext.h>
#include <wx/sizer.h>
#include <wx/datectrl.h> // 确保包含日期选择器头文件

BEGIN_EVENT_TABLE(ReturnBookDialog, wxDialog)
    EVT_BUTTON(wxID_OK, ReturnBookDialog::OnOK)
    EVT_BUTTON(wxID_CANCEL, ReturnBookDialog::OnCancel)
END_EVENT_TABLE()

ReturnBookDialog::ReturnBookDialog(wxWindow* parent)
    : wxDialog(parent, wxID_ANY, wxT("图书归还"), wxDefaultPosition, wxSize(400, 250)) {

    wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);

    wxFlexGridSizer* fgs = new wxFlexGridSizer(2, 5, 9);

    fgs->Add(new wxStaticText(this, wxID_ANY, wxT("借阅ID")), 0, wxALIGN_CENTER_VERTICAL);
    m_borrowIdCtrl = new wxTextCtrl(this, wxID_ANY);
    fgs->Add(m_borrowIdCtrl, 1, wxEXPAND);

    fgs->Add(new wxStaticText(this, wxID_ANY, wxT("实际归还日期")), 0, wxALIGN_CENTER_VERTICAL);
    m_returnDateCtrl = new wxDatePickerCtrl(this, wxID_ANY, wxDefaultDateTime, wxDefaultPosition, wxDefaultSize, wxDP_DROPDOWN | wxDP_SHOWCENTURY);
    fgs->Add(m_returnDateCtrl, 1, wxEXPAND);

    vbox->Add(fgs, 1, wxALL | wxEXPAND, 10);

    wxBoxSizer* btnSizer = new wxBoxSizer(wxHORIZONTAL);
    btnSizer->Add(new wxButton(this, wxID_OK, wxT("确定")), 0, wxALL, 5);
    btnSizer->Add(new wxButton(this, wxID_CANCEL, wxT("取消")), 0, wxALL, 5);

    vbox->Add(btnSizer, 0, wxALIGN_CENTER);

    SetSizer(vbox);
    Centre();
}

std::pair<std::string, time_t> ReturnBookDialog::GetReturnInfo() const {
    // 获取实际归还日期并格式化为 time_t
    wxDateTime returnDate = m_returnDateCtrl->GetValue();
    time_t dateTicks = returnDate.GetTicks();

    return std::make_pair(m_borrowIdCtrl->GetValue().ToStdString(), dateTicks);
}

void ReturnBookDialog::OnOK(wxCommandEvent& event) {
    EndModal(wxID_OK);
}

void ReturnBookDialog::OnCancel(wxCommandEvent& event) {
    EndModal(wxID_CANCEL);
}
